package com.ccl.iot.managers;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import cn.com.broadlink.sdk.BLLet;
import cn.com.broadlink.sdk.constants.BLAppSdkErrCode;
import cn.com.broadlink.sdk.result.account.BLLoginResult;

public class BLUserInfoMamager {

    private static BLUserInfoMamager mInstance;
    private final UserManager mUserManager;

    public static BLUserInfoMamager Init(Context context) {
        if (mInstance == null) {
            mInstance = new BLUserInfoMamager(context);
        }
        return mInstance;
    }

    public static BLUserInfoMamager GetDefaultManager() {
        return mInstance;
    }

    private SharedPreferences mSharedPreferences;

    /**
     * 用户ID
     **/
    private String userid;

    /**
     * 用户session
     **/
    private String loginsession;

    /**
     * 昵称
     **/
    private String nickname;

    /**
     * 头像地址
     **/
    private String iconpath;

    /**
     * 登录ID
     **/
    private String loginip;

    /**
     * 登录时间
     **/
    private String logintime;

    /**
     * 性别
     **/
    private String sex;

    private UserManager.UserEventListener mUserEventListener = new UserManager.UserEventListener() {
        @Override
        public void onLoginEvent(boolean aCanceled) {

            if (!TextUtils.isEmpty(userid)
                    && !TextUtils.isEmpty(loginsession)) {

                //本地登录
                BLLoginResult loginResult = new BLLoginResult();
                loginResult.setUserid(userid);
                loginResult.setLoginsession(loginsession);
                loginResult.setIconpath(iconpath);
                loginResult.setNickname(nickname);
                loginResult.setSex(sex);
                loginResult.setLoginip(loginip);
                loginResult.setLogintime(logintime);
                BLLet.Account.localLogin(loginResult);
            } else {
                BLLoginResult blLoginResult = BLLet.Account.thirdAuth(String.valueOf(mUserManager.getLogonUser()));
                if (blLoginResult != null && blLoginResult.getError() == BLAppSdkErrCode.SUCCESS) {
                    //保存登录信息
                    login(blLoginResult.getUserid(),
                            blLoginResult.getLoginsession(), blLoginResult.getNickname(),
                            blLoginResult.getIconpath(), blLoginResult.getLoginip(),
                            blLoginResult.getLogintime(), blLoginResult.getSex());
                }
            }
        }

        @Override
        public void onPrepareLogout() {

        }

        @Override
        public void onLogout(boolean aOK) {
            loginOut();
//            BLLet.Controller.finish();
        }
    };

    public BLUserInfoMamager(Context context) {
        mSharedPreferences = context.getSharedPreferences("UserInfo", Context.MODE_PRIVATE);
        this.userid = mSharedPreferences.getString("userid", null);
        this.loginsession = mSharedPreferences.getString("loginsession", null);
        this.nickname = mSharedPreferences.getString("nickname", null);
        this.iconpath = mSharedPreferences.getString("iconpath", null);
        this.loginip = mSharedPreferences.getString("loginip", null);
        this.logintime = mSharedPreferences.getString("logintime", null);
        this.sex = mSharedPreferences.getString("sex", null);

        mUserManager = UserManager.GetDefaultManager();
        if (mUserManager != null) {
            mUserManager.addUserEventListener(mUserEventListener);
        }
    }

    public void login(String userid, String loginsession, String nickname, String iconpath, String loginip,
                      String logintime, String sex) {
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        editor.putString("userid", userid);
        editor.putString("loginsession", loginsession);
        editor.putString("nickname", nickname);
        editor.putString("iconpath", iconpath);
        editor.putString("loginip", loginip);
        editor.putString("logintime", logintime);
        editor.putString("sex", sex);
        editor.commit();
        this.userid = userid;
        this.loginsession = loginsession;
        this.nickname = nickname;
        this.iconpath = iconpath;
        this.loginip = loginip;
        this.logintime = logintime;
        this.sex = sex;
    }

    public void loginOut() {
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        editor.putString("userid", null);
        editor.putString("loginsession", null);
        editor.putString("nickname", null);
        editor.putString("iconpath", null);
        editor.putString("loginip", null);
        editor.putString("logintime", null);
        editor.putString("sex", null);
        editor.putString("flag", null);
        editor.commit();
        this.userid = null;
        this.loginsession = null;
        this.nickname = null;
        this.iconpath = null;
        this.loginip = null;
        this.logintime = null;
        this.sex = null;
    }

    public String getUserid() {
        return userid;
    }

    public String getLoginsession() {
        return loginsession;
    }

    public String getNickname() {
        return nickname;
    }

    public String getIconpath() {
        return iconpath;
    }

    public String getLoginip() {
        return loginip;
    }

    public String getLogintime() {
        return logintime;
    }

    public String getSex() {
        return sex;
    }

    public boolean isLogin(){
        return !TextUtils.isEmpty(userid) &&!TextUtils.isEmpty(loginsession);
    }

}
